import Service.simpleBankSystem;
import Util.ProcessFinancialProductsJob;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import java.util.TimeZone;

public class Main {
    private static final Logger logger = LogManager.getLogger(Main.class);

    private static void startFinancialProductScheduler() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();

            JobDetail job = JobBuilder.newJob(ProcessFinancialProductsJob.class)
                    .withIdentity("financialProductJob", "financialGroup")
                    .build();

            // 设置触发器，每天 00:00 运行，时区为 UTC+8
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("dailyTrigger", "financialGroup")
                    .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(0, 0)
                            .inTimeZone(TimeZone.getTimeZone("UTC+8"))) // 设置时区为 UTC+8
                    .build();

            scheduler.scheduleJob(job, trigger);

            logger.info("理财产品理财检查定时任务调度已启动");
        } catch (SchedulerException e) {
            logger.error("调度器启动错误: {}", e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        simpleBankSystem simpleBankSystem = new simpleBankSystem();
        startFinancialProductScheduler();
        simpleBankSystem.showMenu();
    }
}